package com.fr.data.core.db.dialect;

import java.sql.Connection;

public class DB2Dialect extends AbstractDialect
{
  private int db2FetchSize = 50;
  private String defaultAutoIncrease = "GENERATED ALWAYS AS IDENTITY (START WITH 1,INCREMENT BY 1,MINVALUE 1,NO CACHE)";

  public DB2Dialect()
  {
    this.left_quote = "\"";
    this.right_quote = "\"";
  }

  public String columnType2SQL(int paramInt, String paramString)
  {
    switch (paramInt)
    {
    case -7:
      return "varchar(1)";
    case 4:
      return "integer";
    case 12:
      return "varchar(" + paramString + ")";
    case -3:
      return "varbinary(" + paramString + ")";
    case -6:
      return "smallint";
    case 3:
      return "decimal(" + paramString + ")";
    case 2:
      return "numeric(" + paramString + ")";
    case 1:
      return "char(" + paramString + ")";
    case -1:
      return "long varchar";
    case -4:
      return "long varchar";
    case 2004:
      return "blob(" + paramString + ")";
    }
    return TypeUtils.getTypeName(paramInt);
  }

  public int getFetchSize()
  {
    return this.db2FetchSize;
  }

  public void setFetchSize(int paramInt)
  {
    this.db2FetchSize = paramInt;
  }

  public String createSequence(Connection paramConnection, String paramString1, String paramString2, String paramString3)
  {
    return paramString3 + " " + this.defaultAutoIncrease;
  }

  public String getIdentitySelectString()
  {
    return "values identity_val_local()";
  }

  public String getLimitString(String paramString, int paramInt1, int paramInt2)
  {
    int i = paramString.toLowerCase().indexOf("select");
    StringBuffer localStringBuffer = new StringBuffer(paramString.length() + 100).append(paramString.substring(0, i)).append("select * from ( select ").append(getRowNumber(paramString));
    if (hasDistinct(paramString))
      localStringBuffer.append(" row_.* from ( ").append(paramString.substring(i)).append(" ) as row_");
    else
      localStringBuffer.append(paramString.substring(i + 6));
    localStringBuffer.append(" ) as temp_ where rownumber_ ");
    localStringBuffer.append("between " + (paramInt1 + 1) + " and " + paramInt2);
    return localStringBuffer.toString();
  }

  private static boolean hasDistinct(String paramString)
  {
    return (paramString.toLowerCase().indexOf("select distinct") >= 0);
  }

  private String getRowNumber(String paramString)
  {
    StringBuffer localStringBuffer = new StringBuffer(50).append("rownumber() over(");
    int i = paramString.toLowerCase().indexOf("order by");
    if ((i > 0) && (!(hasDistinct(paramString))))
      localStringBuffer.append(paramString.substring(i));
    localStringBuffer.append(") as rownumber_,");
    return localStringBuffer.toString();
  }

  public boolean supportsLimit()
  {
    return true;
  }

  public String quartzDelegateClass()
  {
    return "";
  }
}